import log_proc as log
from basic_operations import BasicOperations
from constants import *
import time

class MainLine(BasicOperations):
    def __init__(self, mapNumber, diffLevel, teamNumber, stepCount, borderNumber):
        self.mapNo = mapNumber
        self.difficulty = diffLevel
        self.teamNo = teamNumber
        self.steps = stepCount
        self.borderNum = borderNumber

    # 主线地图中执行前进一次
    def normalBattleOnce(self, action):
        log.info("- 开始一个节点")
        time.sleep(1)

        # 1.可能有选阵型
        # 2.一定有，等战斗结果,多次点击
        topLoc1 = []
        topLoc2 = []
        topLoc6 = []
        topLoc7 = []
        attempt = 0
        while attempt < 3*ATTLIMIT:
            targetPath = self.autoScreemShot()
            topLoc1, botLoc1 = self.checkLocationOnly(targetPath, NAME_FORMAT + DEFAULT_FORMAT)  # 选阵型
            topLoc2, botLoc2 = self.checkLocationOnly(targetPath, NAME_BATTLERESULT)  # 战斗结果
            topLoc6, botLoc6 = self.checkLocationOnly(targetPath, NAME_HOST)  # 没走完给定步数，进入末尾资源点，结束重置到本丸界面
            topLoc7, botLoc7 = self.checkLocationOnly(targetPath, action)  # 下一步 的图标检测

            if len(topLoc1) == 0 and len(topLoc2) == 0 and len(topLoc6) == 0 and len(topLoc7) == 0:
                self.inputLocationAndClick(DEFAULT_TOPLOC,DEFAULT_BOTLOC) # 如果出现减肥，点击跳过
                time.sleep(3)
                attempt = attempt + 1
            else:
                break

        if len(topLoc7) != 0: #直接下一步
            log.info("没有检测到 战斗结果，直接下一步")
        elif len(topLoc2) != 0:  # 战斗结果
            # 点击,跳过掉刀，进入3，4
            clickNum = 2
            while clickNum > 0:
                self.inputLocationAndClick(DEFAULT_TOPLOC, DEFAULT_BOTLOC)
                clickNum = clickNum - 1
        elif len(topLoc1) != 0:  # 选阵型
            # 选择阵型
            self.inputLocationAndClick(topLoc1, botLoc1)

            if not self.toNextClick(NAME_FORMAT_CONF):
                log.info("未识别到 阵型确认")
                return False

            # 检测战斗结果, 点击，进入3，4
            if not self.toNextClick(NAME_BATTLERESULT, True, 3, 2*ATTLIMIT):
                log.info("阵型选择后，未识别到 战斗结果")
                return False
        elif len(topLoc6) != 0:
            log.info("进入末尾，提前回城")
            return False
        else:
            log.info("未识别到下一步，" + NAME_FORMAT + DEFAULT_FORMAT + " 或者 " + NAME_BATTLERESULT)
            return False

        # 3.可能有掉刀，在战斗结果画面左侧反复点击可进入下一步
        # 4.一定有，继续进军 或 返回本丸
        attempt = 0
        while attempt < ATTLIMIT:
            targetPath = self.autoScreemShot()
            topLoc5, botLoc5 = self.checkLocationOnly(targetPath, action)
            topLoc6, botLoc6 = self.checkLocationOnly(targetPath, NAME_HOST)  # 走过王点或没走完给定步数，进入资源点，结束重置到本丸界面

            if len(topLoc5) == 0 and len(topLoc6) == 0 :
                self.inputLocationAndClick(DEFAULT_TOPLOC,DEFAULT_BOTLOC) # 如果掉刀，点击跳过
                time.sleep(3)
                attempt = attempt + 1
            else:
                break

        if len(topLoc5) != 0:
            if action == NAME_CONTBATTLE:
                if self.moveCheck():
                    self.inputLocationAndClick(topLoc5, botLoc5)
                    return True
                else:
                    log.info("前方是王点，停止前进")
                    return False
            else:
                self.inputLocationAndClick(topLoc5, botLoc5)
                return True
        elif len(topLoc6) != 0:
            log.info("进入末尾，提前回城")
            return False
        else:
            log.info("未检测到 下一步 或 本丸")
            return False

    # 进入主线地图a（输入值）,用队伍b(输入值)，出阵一次，踏过c(输入值)个节点，返回bool
    def upgradeOnce(self):
        maxLoc, bottomRight = self.checkLocation(NAME_HOST)
        # 日常练级
        # 1. 检测到主界面，进入剧情出阵
        if len(maxLoc) == 0:
            log.info("未找到本丸")
            return False

        self.inputLocationAndClick(DEFAULT_TOPLOC, DEFAULT_BOTLOC)
        maxLoc, bottomRight = self.checkLocation(NAME_ENTERFILED)
        if len(maxLoc) == 0:
            log.info("未找到侧栏出阵")
            return False

        self.inputLocationAndClick(maxLoc, bottomRight)
        # 2. 进入练级地图
        maxLoc, bottomRight = self.checkLocation(NAME_NORMALENTERFILED)

        if len(maxLoc) == 0:
            log.info("未找到出阵界面主线出阵图标")
            return False

        self.inputLocationAndClick(maxLoc, bottomRight)
        # 3. 选择练级地图（可输入）
        # if not self.toNextClick(NAME_NORMALBATTLE + "_" + self.mapNo):
        #     log.info("未找到 地图：" + NAME_NORMALBATTLE + "_" + self.mapNo)
        #     return True

        # 4. 选择地图难度（可输入）
        if not self.toNextClick(NAME_NORMALBATTLE + "_" + self.mapNo + self.difficulty):
            log.info("未找到 地图难度：" + NAME_NORMALBATTLE + "_" + self.mapNo + self.difficulty)
            return True

        # 5. 选择练级队伍（可输入）
        # maxLoc, bottomRight = self.checkLocationOnce(NAME_CHOOSETEAM)
        # if len(maxLoc) != 0:
        #     self.inputLocationAndClick(maxLoc, bottomRight)
        # if not self.toNextClick(NAME_TEAM + self.teamNo):  # 模式匹配出错了，需要修改
        #     log.info("未找到 队伍"+str(self.teamNo)+" 图标")
        #     return True

        if not self.toNextClick(NAME_STARTBATTLE):
            log.info("未找到 即刻出阵 图标")
            return True

        if self.steps > 0:  #前进指定步数
            step = 1
            commonReturn = True
            while step < self.steps:
                if self.normalBattleOnce(NAME_CONTBATTLE):
                    step = step + 1
                else:
                    log.info("道中继续前进失败")
                    commonReturn = False
                    break
            if step == self.steps and commonReturn:
                if self.normalBattleOnce(NAME_RETURNHOST):
                    if not self.toNextClick(NAME_RETURNYES):
                        log.info("回城确认失败")
                else:
                    log.info("未找到 返回本丸 图标")
        else:   # 直接走到王点
            while True:
                if not self.normalBattleOnce(NAME_CONTBATTLE):
                    log.info("继续前进结束")
                    break

        return True

    # 重伤检测
    def seriousInjuryDetection(self):
        pass

    # 前进检测，识别是否在王点前
    def moveCheck(self):
        if self.borderNum<=0:
            return True

        res = True
        for i in range(self.borderNum):
            iconName = NAME_NORMALBATTLE + "_" + self.mapNo + self.difficulty + NAME_TAIL_BOUND + str(i)
            if self.boundaryDetection(iconName):
                res = False
                break
        return res
